using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Liebrand.UrlShortener
{
    public static class SharePointExtensions
    {

        #region Methods

        public static bool ListExists(this SPWeb web, string listName)
        {
            var lists = web.Lists;
            foreach (SPList list in lists)
            {
                if (list.Title.Equals(listName))
                    return true;
            }

            return false;
        }

        public static void AddField(this SPList list, string displayName, string description, SPFieldType fieldType, bool required)
        {
            if (list.Fields.ContainsField(displayName))
                return;

            list.Fields.Add(displayName, fieldType, required);
            var field = list.Fields[displayName];
            if (!string.IsNullOrEmpty(description))
                field.Description = description;

            field.Update();
        }
        #endregion

    }
}
